package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.mapper.ClazzMapper;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.pojo.StudentQueryParam;
import com.itheima.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;
    @Autowired
    private ClazzMapper clazzMapper;

    /**
     * 分页查询学员信息
     * @param sqp
     * @return
     */
    @Override
    public PageBean page(StudentQueryParam sqp) {
//        1.设置分页参数
        PageHelper.startPage(sqp.getPage(), sqp.getPageSize());
//        2.调用Mapper层方法返回List<student>集合
        Page<Student> page = (Page<Student>)studentMapper.page(sqp);
//        3.返回PageBean对象
        return new PageBean(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除员工
     * @param ids
     */
    @Override
    public void delete(List<Integer> ids) {
//        1.调用Mapper层方法
        studentMapper.deleteByIds(ids);
    }


    /**
     * 新增员工
     * @param student
     */
    @Override
    public void add(Student student) {
//        1.学员设置创建时间和更新时间
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());

//        2.调用Mapper层方法
        studentMapper.insert(student);
    }

    /**
     * 根据ID回显学员信息
     * @param id
     * @return
     */
    @Override
    public Student getInfo(Integer id) {
//        1.调用学员Mapper层方法查询信息
        Student student = studentMapper.getInfoById(id);

//        2.调用班级Mapper查询学员班级
        String clazzName = clazzMapper.getClazzNameById(id);

//        3.合并同一个学生对象
        student.setClazzName(clazzName);

//        4.返回Student对象
        return student;
    }

    /**
     * 学员信息修改
     * @param student
     */
    @Override
    public void update(Student student) {
//        1.修改学员更新时间
        student.setUpdateTime(LocalDateTime.now());

//        2. 调用Mapper层方法
        studentMapper.update(student);
    }


    /**
     * 违纪处理
     * @param id
     * @param score
     */
    @Override
    public void violation(Integer id, Integer score) {

//        1.更新学员违纪分数（包括最新更改时间）
        studentMapper.violation(id, score);

    }


}
